package com.gc.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 管理员界面过滤器
 */
@WebFilter(filterName = "InLogFilter", urlPatterns = { "/login.jsp" })
public class InLogFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpSession session = request.getSession();
        if(session.getAttribute("Authority") != null ) {
            String authority = (String)session.getAttribute("Authority");
            HttpServletResponse response =  (HttpServletResponse)servletResponse;
            //3秒后跳转
            if(authority.equals("1")) {
                response.setHeader("refresh", "3;URL=/admin/indexAdmin.jsp");
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write("<h3>您已登录！请先登出！3秒后回到主页面</h2>");
            } else if(authority.equals("2")) {
                response.setHeader("refresh", "3;URL=/admin/indexStudent.jsp");
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write("<h3>您已登录！请先登出！3秒后回到主页面</h2>");
            } else if(authority.equals("3")){
                response.setHeader("refresh", "3;URL=/admin/indexTeacher.jsp");
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write("<h3>您已登录！请先登出！3秒后回到主页面</h2>");
            } else if(authority.equals("4")){
                response.setHeader("refresh", "3;URL=/admin/indexBookSeller.jsp");
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write("<h3>您已登录！请先登出！3秒后回到主页面</h2>");
            }

        } else {
            filterChain.doFilter(servletRequest,servletResponse);
        }


    }

    @Override
    public void destroy() {

    }


}
